/**
 * Created by Administrator on 2018/5/9.
 * 操作的公共js
 */

function emptyStringConvert(str) {
    if (null == str || '' == str) {
        return '';
    } else {
        return str;
    }
}
/**
 *生产订单转单
 * @param tableid
 * @param id
 * @returns
 */
function transferOrderAjaxSubmit(tableid, id){
    debugger
    var ids=[];
	 if (null != tableid && "" !== tableid){
	        var datas = $('#' + tableid).dataTableExtend('getCheck');
	        if (datas.length === 0) {
	            DGG.Warning('请选择一个订单！');
	            return;
	        }
	        datas.forEach((v,index)=>{
	            ids.push(v.id)
            })
	 } else {
         ids.push(id)
     }
     DGG.loading();
     $.post(sysInfo.basePath + "/sc/receives/transferOrderPage.html",{ids:ids.toString(),type:"transferOrder"},function(html){
  	     DGG.Open({
	            content:html,
	            title:"订单转单",
	            area:'500px',
	            yes:function(index,layero){
	             	 DGG.loading();
	            	 var return_remark = $("#transfer_order_remark").val();
	            	 if(return_remark.length>200){
	            		 DGG.Warning("备注内容不能超过200字");
						 DGG.loading("close");
						return false;
	            	 }
					var param =$("#transferOrderBoxForm").serializeObject();//获取退回参数
					param.ids=ids.toString();
					$.post(sysInfo.basePath+"/sc/receives/transfer_order.do",param,function(result){
						if(result.code==0){
							DGG.Succeed('转单成功');
							window.location.reload();
						}else{
							DGG.Errors(result.msg);
							DGG.loading("close");
						}
					},'json');
	            }
	        })

       },'html');
    // $.ajax({
    //     type: 'POST',
    //     async: false,
    //     url: sysInfo.basePath + '/zzsc/inprocess/validation_update_node.do',
    //     data:{id:id},
    //     dataType: 'JSON',
    //     success: function (obj) {
    //         DGG.loading('close');
    //         if (obj.code === 0) {
    //             DGG.loading();
    //             $.post( sysInfo.basePath+"/zzsc/inprocess/open_update_node.html",{id:ids[0]},function (data) {
    //                 dgg.Open({
    //                     title: '更新节点',
    //                     content: data ,
    //                     area: '700px',
    //                     yes:function () {
    //                         //updateNodeZz();
    //                     }
    //                 });
    //             },'html');
    //         } else {
    //             DGG.Errors(obj.msg);
    //         }
    //     },
    //     error: function () {
    //         DGG.loading('close');
    //         DGG.Errors('系统异常,请联系管理员!');
    //     }
    // });
}
/**
 *创建工单
 * @param tableid
 * @param id
 * @returns
 */
function createWorkOrder(tableid, url) {
    if (null != tableid && "" !== tableid) {
        var datas = $('#' + tableid).dataTableExtend('getCheck');
        if (datas.length === 0) {
            DGG.Warning('请选择一个订单！');
            return;
        }
        if (datas.length > 1) {
            DGG.Warning('只能选择一个订单！');
            return;
        }
        var scProductOrderId = datas[0].id;
        var scProductOrderNo = datas[0].scProductOrderNo;
        var customerId = datas[0].customerId;
        var source = "BUS_YT_ZZ";
        var orderId=datas[0].orderId;
        window.parent.updateTab("创建工单",url+"?scProductOrderId="+scProductOrderId+
            "&scProductOrderNo="+scProductOrderNo+"&customerId="+customerId+"&source="+source+"&orderId="+orderId);
    }
}
function createWorkOrderInfo(id,scProductOrderNo,customerId,url){
    var source = "BUS_YT_ZZ";
    window.parent.updateTab("创建工单",url+"?scProductOrderId="+id+
        "&scProductOrderNo="+scProductOrderNo+"&customerId="+customerId+"&source="+source);
}
/**
 * 费用申请
 * @param id
 * @param no
 */
function applyMoneyProduct(tableid, orderBusNo,orderBusId) {
    if (null != tableid && "" !== tableid) {
        var datas = $('#' + tableid).dataTableExtend('getCheck');
        if (datas.length > 1) {
            DGG.Warning('只能选择一个订单！');
            return;
        }
        if (datas.length === 0) {
            DGG.Warning('请选择一个订单！');
            return;
        }
        orderBusNo = datas[0].orderBusNo;
        orderBusId = datas[0].orderBusId;
    }
    // window.parent.updateTab("费用申请：" + orderBusNo, orfApiUrl + "/orf/outlay/addoutlay.html?orderBusinessId=" + orderBusId + "&type=1");
    // 新的费用申请
    window.parent.updateTab('费用申请:' + orderBusNo, orfNewApiUrl + "/outlay/apply_outlay.html");
}
/**
 *  打开更新节点界面
 *   tableid table的id
 *   id   数据id
 *   mqy
 */
function openUpdateNode(tableid, id) {
    if (null != tableid && "" !== tableid) {
        var ids = scCommonGetCheckedallproorder(tableid);
        if (ids.length > 1) {
            DGG.Warning('只能选择一个订单！');
            return;
        }
        if (ids.length === 0) {
            DGG.Warning('请选择一个订单！');
            return;
        }
        DGG.loading();
        //判断该条数据弹出那个业态的界面
        $.ajax({
            type: 'POST',
            async: false,
            url: sysInfo.basePath + '/zzsc/inprocess/validation_update_node.do',
            data:{id:ids[0]},
            dataType: 'JSON',
            success: function (obj) {
                DGG.loading('close');
                if (obj.code === 0) {
                    DGG.loading();
                    $.post( sysInfo.basePath+"/zzsc/inprocess/open_update_node.html",{id:ids[0]},function (data) {
                        dgg.Open({
                            title: '更新节点',
                            content: data ,
                            area: '850px',
                            yes:function () {
                                updateNodeZz();
                            }
                        });
                    },'html');
                } else {
                    DGG.Errors(obj.msg);
                }
            },
            error: function () {
                DGG.loading('close');
                DGG.Errors('系统异常,请联系管理员!');
            }
        });
    }else {//详情里面
        DGG.loading();
        $.ajax({
            type: 'POST',
            async: false,
            url: sysInfo.basePath + '/zzsc/inprocess/validation_update_node.do',
            data:{id:id},
            dataType: 'JSON',
            success: function (obj) {
                DGG.loading('close');
                if (obj.code === 0) {
                    DGG.loading();
                    $.post( sysInfo.basePath+"/zzsc/inprocess/open_update_node.html",{id:id},function (data) {
                        dgg.Open({
                            title: '更新节点',
                            content: data ,
                            area: '700px',
                            yes:function () {
                                updateNodeDetailZz();
                            }
                        });
                    },'html');
                } else {
                    DGG.Errors(obj.msg);
                }
            },
            error: function () {
                DGG.loading('close');
                DGG.Errors('系统异常,请联系管理员!');
            }
        });
    }
}


/**
 *  打开生产暂停
 *  2018/8/16  10:18
 *  maoquanyi
 */
function suspendOrder(tableid, id) {
    if (null != tableid && "" != tableid) {
        var ids = scCommonGetCheckedallproorder(tableid);
        if (ids.length > 1) {
            DGG.Warning('只能选择一个订单！');
            return;
        }
        if (ids.length == 0) {
            DGG.Warning('请选择一个订单！');
            return;
        }
        DGG.loading();
        $.post( sysInfo.basePath+"/zzsc/inprocess/suspend_order_page.html",{id:ids[0]},function (data) {
            dgg.Open({
                title: '生产暂停',
                content: data ,
                area: '600px',
                btn:["确认暂停","取消"],
                yes:function () {
                    suspendProOrder();
                }
            });
        },'html');
    } else {
        DGG.loading();
        $.post( sysInfo.basePath+"/zzsc/inprocess/suspend_order_page.html",{id:id},function (data) {
            dgg.Open({
                title: '生产暂停',
                content: data ,
                area: '600px',
                btn:["确认暂停","取消"],
                yes:function () {
                    suspendProOrder();
                }
            });
        },'html');
    }
}

/**
 * 打开退单
 */
function chargeBackOrder(tableid, id) {
    // debugger;
    if (null != tableid && "" != tableid) {
        var ids = scCommonGetCheckedallproorder(tableid);
        if (ids.length > 1) {
            DGG.Warning('只能选择一个订单！');
            return;
        }
        if (ids.length == 0) {
            DGG.Warning('请选择一个订单！');
            return;
        }
        DGG.loading();
        $.post( sysInfo.basePath+"/zzsc/inprocess/charge_back_order_page.html",{id:ids[0]},function (data) {
            dgg.Open({
                title: '申请退单',
                content: data ,
                area: '500px',
                btn:["确认退单","取消"],
                yes:function () {
                    abolishOrChargeBack();
                }
            });
        },'html');
    } else {
        DGG.loading();
        $.post( sysInfo.basePath+"/zzsc/inprocess/charge_back_order_page.html",{id:id},function (data) {
            dgg.Open({
                title: '申请退单',
                content: data ,
                area: '500px',
                btn:["确认退单","取消"],
                yes:function () {
                    abolishOrChargeBack();
                }
            });
        },'html');
    }
}
/**
 *
 * 退单确定方法
 * @param type
 */
function abolishOrChargeBack() {
    var abandonReason = $("#abandonReason").val();
    if (checkIsEmpty(abandonReason)){
        DGG.Warning("请选择申请理由!");
        return;
    }
    var auditorId = $("#auditorId").val();
    if (checkIsEmpty(auditorId)){
        DGG.Warning("请选择审核人!");
        return;
    }
    var remark = $("#remark").val();
    var id = $("#id").val();
    DGG.Confire("确认退单吗？", "退单", function () {
        DGG.loading();
        $.ajax({
            url: sysInfo.basePath + '/zzsc/inprocess/abandon_reason_order.do',
            data: {id:id,abandonReason:abandonReason,auditorId:auditorId,remark:remark},
            type: "POST",
            dataType: "json",
            success: function (obj) {
                DGG.loading('close');
                if (obj.code == 1) {
                    DGG.Errors(obj.msg);
                } else {
                    DGG.Succeed(obj.msg);
                    window.location.reload();
                }
            }
        });
    });
}


/**
 * 添加协单人员
 * @param tableid
 * maoquanyi
 */
function addAssist(tableid, id) {
    if (null != tableid && "" !== tableid) {//从列表进来
        var ids = scCommonGetCheckedallproorder(tableid);
        if (ids.length > 1) {
            DGG.Warning('只能选择一个订单！');
            return;
        }
        if (ids.length == 0) {
            DGG.Warning('请选择一个订单！');
            return;
        }
        DGG.loading();
        $.post( sysInfo.basePath+"/zzsc/inprocess/add_assist_page.html",{id:ids[0]},function (data) {
            dgg.Open({
                title: '添加协单人员',
                content: data ,
                area: '700px',
                btn:["确认","取消"],
                yes:function () {
                    updateAssist(ids[0]);
                }
            });
        },'html');
    } else {//从详情进来
        DGG.loading();
        $.post( sysInfo.basePath+"/zzsc/inprocess/add_assist_page.html",{id:id},function (data) {
            dgg.Open({
                title: '添加协单人员',
                content: data ,
                area: '700px',
                btn:["确认","取消"],
                yes:function () {
                    updateAssist(id);
                }
            });
        },'html');
    }
}
/**
 * 协单人员点确定  参数productOrderId 为生产订单的ID
 * mqy
 */
function updateAssist(productOrderId) {
    var assistIds = getVal('#assistId',0);
    DGG.loading();
    $.ajax({
        url: sysInfo.basePath + '/zzsc/inprocess/update_assist.do',
        data: {assistIds: assistIds, productOrderId: productOrderId},
        type: "POST",
        dataType: "json",
        success: function (obj) {
            DGG.loading('close');
            if (obj.code != 0) {
                DGG.Errors(obj.msg);
            } else {
                DGG.Succeed(obj.data);
                layer.closeAll();
                window.location.reload();
            }
        }
    });
}

/**
 *  详情里面的编辑
 *  2018/8/21  10:09
 *  mqy
 **/
function editProductOrder() {
    DGG.loading();
    $.ajax({
        url: sysInfo.basePath + '/zzsc/inprocess/check_is_flower_or_assister.do',
        data: {scOrderId:$('#scOrderId').val()},
        type: "POST",
        dataType: "json",
        success: function (obj) {
            DGG.loading('close');
            if (obj.code == 1) {
                DGG.Errors(obj.msg);
            } else {
                //处理是否可编辑和按钮切换
                $("#productDataCom .table-input").removeProp('readonly').removeProp('disabled').removeClass('productDataClass');
                $("#productDataCom .textarea-class").removeProp('readonly').removeProp('disabled').removeClass('productDataClass');
                $("#editProductOrder").addClass('none');
                $("#saveProductOrder").removeClass('none');
                //处理颜色
                $("#productDataCom").find(".Wdate").css('background-color','#fff');
            }
        }
    });

}
/**
 *  详情里面的点了编辑之后的保存
 *  2018/8/21  10:09
 *  mqy
 **/
function saveProductOrder() {
    //先验证数字是否满足条件
    if (!isFloatRangeSam('#channelRebates',2,'99999999.99')){//验证渠道返点
        return false;
    }
    if (!isFloatRangeSam('#foreignRebates',2,'99999999.99')){//验证对外返点
        return false;
    }
    var serializeObject = $("#productData").serializeObject();
    serializeObject.scOrderId = $('#scOrderId').val();
    DGG.loading();
    $.ajax({
        url: sysInfo.basePath + '/zzsc/inprocess/save_product_order_data.do',
        data: serializeObject,
        type: "POST",
        dataType: "json",
        success: function (obj) {
            DGG.loading('close');
            if (obj.code == 1) {
                DGG.Errors(obj.msg);
            } else {
                DGG.Succeed(obj.data);
                //处理是否可编辑和按钮切换
                $("#productDataCom .table-input").not('.Wdate').prop('readonly','readonly').addClass('productDataClass');
                $("#productDataCom .textarea-class").prop('readonly','readonly').addClass('productDataClass');
                $("#productDataCom .Wdate").prop('disabled','disabled').css('background-color','#dadada');
                $("#saveProductOrder").addClass('none');
                $("#editProductOrder").removeClass('none');
            }
        }
    });
}

/**
 *  移交
 *  2018/9/12  10:09
 *  mqy   certificate  appointment
 **/
function turnoverProductOrder(tableid,id) {
    if (null != tableid && "" !== tableid) {//从列表进来
        var ids = scCommonGetCheckedallproorder(tableid);
        if (ids.length === 0) {
            DGG.Warning('请选择一个订单！');
            return;
        }
        var allIdStr = ids.join(",");
        DGG.loading();
        $.post( sysInfo.basePath+"/zzsc/inprocess/turn_over_product_order.html",{id:allIdStr},function (data) {
            dgg.Open({
                title: '移交',
                content: data ,
                area: '500px',
                btn:["确定","取消"],
                yes:function () {
                    saveTurnOver(allIdStr);
                }
            });
        },'html');
    } else {//从详情进来
        if (checkIsEmpty(id)){
            DGG.Warning('未获取到生产订单ID,请刷新列表！');
            return;
        }
        DGG.loading();
        $.post( sysInfo.basePath+"/zzsc/inprocess/turn_over_product_order.html",{id:id},function (data) {
            dgg.Open({
                title: '移交',
                content: data ,
                area: '500px',
                btn:["确定","取消"],
                yes:function () {
                    saveTurnOver(id);
                }
            });
        },'html');
    }
}


/**
 *  证书匹配
 *  2018/8/27  10:09
 *  mqy   certificate  appointment
 **/
function certificateMatch(tableid,id) {
    if (null != tableid && "" !== tableid) {//从列表进来
        var ids = scCommonGetCheckedallproorder(tableid);
        if (ids.length > 1) {
            DGG.Warning('只能选择一个订单！');
            return;
        }
        if (ids.length == 0) {
            DGG.Warning('请选择一个订单！');
            return;
        }
        DGG.loading();
        $.post( sysInfo.basePath+"/zzsc/inprocess/certificate_matching_page.html",{id:ids[0]},function (data) {
            var layerIndex=dgg.Open({
                title: '证书匹配',
                content: data ,
                area: '80%',
                btn:["取消"],
                success:function(layero, index){
                    $('#layerIndex').val(index);
                },
                yes:function (index) {
                    layer.close(index);
                    DGG.loading('close');
                }
            });
        },'html');
    } else {//从详情进来
        if (checkIsEmpty(id)){
            DGG.Warning('未获取到生产订单ID,请刷新列表！');
            return;
        }
        DGG.loading();
        $.post( sysInfo.basePath+"/zzsc/inprocess/certificate_matching_page.html",{id:id},function (data) {
            var layerIndex=dgg.Open({
                title: '证书匹配',
                content: data ,
                area: '80%',
                btn:["取消"],
                success:function(layero, index){
                    $('#layerIndex').val(index);
                },
                yes:function (index) {
                    layer.close(index);
                    DGG.loading('close');
                }
            });
        },'html');
    }
}
/**
 *  证书匹配弹出框中的匹配按钮
 *  2018/8/28  14:09
 *  mqy
 **/
function openCertificateMatch(id) {
    DGG.loading();
    $.post( sysInfo.basePath+"/zzsc/inprocess/certificate_advance_match_page.html",{id:id},function (data) {
        dgg.Open({
            title: '匹配详情',
            content: data ,
            area: '80%',
            btn:["取消"],
            yes:function (index) {
                layer.close(index);
                DGG.loading('close');
            }
        });
    },'html');
}

//证书取消预定
function cancelAdvanceCertificate(certificateId,certificateNo,scOrderId,pageType,obj) {//pageType 1列表 2 详情  obj详情不用传列表传,用于寻找tr
    DGG.Confire("确定取消预定此证书吗？", '取消预定', function () {
        DGG.loading();
        $.ajax({
            type: 'POST',
            data: {certificateId:certificateId,certificateNo:certificateNo,scOrderId:scOrderId,type:1},//type 1是证书,2是资质
            url: sysInfo.basePath + '/zzsc/inprocess/cancel_advance_certificate.do',
            dataType: 'JSON',
            success: function (data) {
                DGG.loading('close');
                if (data.code != 0) {
                    DGG.Errors(data.msg);
                } else {
                    if (pageType == 1){//删除一行
                        $(obj).parents('.advanceClassTr').remove();
                    } else if (pageType ==2){
                        window.location.reload();
                    }
                    $.ajax({//异步获取已预定证书数量
                        type: 'POST',
                        data: {scOrderId:scOrderId,type:1},//type 1是证书,2是资质
                        url: sysInfo.basePath + '/zzsc/inprocess/query_advance_match_number.do',
                        dataType: 'JSON',
                        success: function (data) {
                            if (data.code != 0) {
                                $('.yudingClass').hide();
                                $('.yuyueClass').hide();
                            } else {
                                var cpNumber = data.data.cpNumber;
                                if (parseInt(cpNumber) > 0){//预定
                                    $('.yudingClass').show();
                                    $('#reservationNum').text(cpNumber);
                                }else {
                                    $('.yudingClass').hide();
                                }
                                var rnNumber = data.data.rnNumber;
                                if (parseInt(rnNumber) > 0){
                                    $('.yuyueClass').show();
                                    $('#appointmentNum').text(rnNumber);
                                } else {
                                    $('.yuyueClass').hide();
                                }
                            }
                        }
                    });
                    DGG.Succeed("操作成功!");
                }
            },
            error: function () {
            }
        });
    });
}
//证书匹配
function matchAdvanceCertificate(certificateId,certificateNo,scOrderId,pageType,obj) {//pageType 1列表 2 详情  obj详情不用传列表传,用于寻找tr
    DGG.Confire("确定匹配此证书吗？", '匹配证书', function () {
        DGG.loading();
        $.ajax({
            type: 'POST',
            data: {certificateId:certificateId,certificateNo:certificateNo,scOrderId:scOrderId,type:1},//type 1是证书,2是资质
            url: sysInfo.basePath + '/zzsc/inprocess/save_match_certificate.do',
            dataType: 'JSON',
            success: function (data) {
                DGG.loading('close');
                if (data.code != 0) {
                    DGG.Errors(data.msg);
                } else {
                    if (pageType == 1){
                        $(obj).parents('.advanceClassTr').remove();
                    } else if (pageType == 2){
                        window.location.reload();
                    }
                    $.ajax({//异步获取已预定证书数量
                        type: 'POST',
                        data: {scOrderId:scOrderId,type:1},//type 1是证书,2是资质
                        url: sysInfo.basePath + '/zzsc/inprocess/query_advance_match_number.do',
                        dataType: 'JSON',
                        success: function (data) {
                            if (data.code != 0) {
                                $('.yudingClass').hide();
                                $('.yuyueClass').hide();
                            } else {
                                var cpNumber = data.data.cpNumber;
                                if (parseInt(cpNumber) > 0){//预定
                                    $('.yudingClass').show();
                                    $('#reservationNum').text(cpNumber);
                                }else {
                                    $('.yudingClass').hide();
                                }
                                var rnNumber = data.data.rnNumber;
                                if (parseInt(rnNumber) > 0){
                                    $('.yuyueClass').show();
                                    $('#appointmentNum').text(rnNumber);
                                } else {
                                    $('.yuyueClass').hide();
                                }
                            }
                        }
                    });
                    DGG.Succeed("操作成功!");
                }
            },
            error: function () {
            }
        });
    });
}
//预约证书
function appointmentCertificate() {
    var certificateType = $("#certificateTypeHigh").val();
    if (checkIsEmpty(certificateType)){
        DGG.Warning("请选择证书类型!");
        return;
    }
    var certificateSpecialtys = $("#certificateSpecialtysHigh").val();
    if (checkIsEmpty(certificateSpecialtys)){
        DGG.Warning("请选择证书专业!");
        return;
    }
    var certificateLevel = $("#certificateLevelHigh").val();
    if (checkIsEmpty(certificateLevel)){
        DGG.Warning("请选择证书等级!");
        return;
    }
    var applicationScopes = $("#applicationScopesHigh").val();
    if (checkIsEmpty(applicationScopes)){
        DGG.Warning("请选择应用范围!");
        return;
    }
    var appointmentAreaId = $("#appointmentAreaId").val();
    if (checkIsEmpty(appointmentAreaId)){
        DGG.Warning("请选择地区!");
        return;
    }
    var serializeObject = $('#myForm').serializeObject();
    //预约证书
    DGG.Confire("确定预约证书吗？", '预约证书', function () {
        DGG.loading();
        $.ajax({
            type: 'POST',
            data: serializeObject,
            url: sysInfo.basePath + '/zzsc/inprocess/save_appointment_certificate.do',
            dataType: 'JSON',
            success: function (data) {
                DGG.loading('close');
                if (data.code != 0) {
                    DGG.Errors(data.msg);
                } else {
                    appoinmentTable.ajax.reload();
                    var $s = $('.myFormClass');
                    $s.find('input').val('');
                    $s.find('select').val('');
                    $s.find('select').val(null).trigger("change");
                    $.ajax({//异步获取已预定证书数量
                        type: 'POST',
                        data: {scOrderId:serializeObject.scOrderId,type:1},//type 1是证书,2是资质
                        url: sysInfo.basePath + '/zzsc/inprocess/query_advance_match_number.do',
                        dataType: 'JSON',
                        success: function (data) {
                            if (data.code != 0) {
                                $('.yudingClass').hide();
                                $('.yuyueClass').hide();
                            } else {
                                var cpNumber = data.data.cpNumber;
                                if (parseInt(cpNumber) > 0){//预定
                                    $('.yudingClass').show();
                                    $('#reservationNum').text(cpNumber);
                                }else {
                                    $('.yudingClass').hide();
                                }
                                var rnNumber = data.data.rnNumber;
                                if (parseInt(rnNumber) > 0){
                                    $('.yuyueClass').show();
                                    $('#appointmentNum').text(rnNumber);
                                } else {
                                    $('.yuyueClass').hide();
                                }
                            }
                        }
                    });
                    DGG.Succeed("操作成功!");
                }
            },
            error: function () {
            }
        });
    });
}

//取消预约证书/资质
function cancelAppointment(scorderId,resevationId,pageType,type) {//pageType 1列表 2 详情  obj详情不用传,列表传,用于寻找tr
    var scOrderId = $('#scOrderId').val();
    if (checkIsEmpty(scOrderId)){
        scOrderId =scorderId;
    }
    //预约证书
    DGG.Confire("确定取消预约吗？", '取消预约', function () {
        DGG.loading();
        $.ajax({
            type: 'POST',
            data: {scOrderId:scOrderId,resevationId:resevationId},
            url: sysInfo.basePath + '/zzsc/inprocess/cancel_appointment_certificate.do',
            dataType: 'JSON',
            success: function (data) {
                DGG.loading('close');
                if (data.code != 0) {
                    DGG.Errors(data.msg);
                } else {
                    if (pageType == 1){
                        appoinmentTable.ajax.reload();
                    } else if (pageType == 2){
                        window.location.reload();
                    }
                    $.ajax({//异步获取已预定证书数量
                        type: 'POST',
                        data: {scOrderId:scorderId,type:type},//type 1是证书,2是资质
                        url: sysInfo.basePath + '/zzsc/inprocess/query_advance_match_number.do',
                        dataType: 'JSON',
                        success: function (data) {
                            if (data.code != 0) {
                                $('.yudingClass').hide();
                                $('.yuyueClass').hide();
                            } else {
                                var cpNumber = data.data.cpNumber;
                                if (parseInt(cpNumber) > 0){//预定
                                    $('.yudingClass').show();
                                    $('#reservationNum').text(cpNumber);
                                }else {
                                    $('.yudingClass').hide();
                                }
                                var rnNumber = data.data.rnNumber;
                                if (parseInt(rnNumber) > 0){
                                    $('.yuyueClass').show();
                                    $('#appointmentNum').text(rnNumber);
                                } else {
                                    $('.yuyueClass').hide();
                                }
                            }
                        }
                    });
                    DGG.Succeed("操作成功!");
                }
            },
            error: function () {
            }
        });
    });
}


/**
 *  证书匹配弹出框中的预约按钮
 *  2018/8/27  10:09
 *  mqy
 **/
function certificateAppointment(id) {
        DGG.loading();
        $.post( sysInfo.basePath+"/zzsc/inprocess/certificate_appointment_page.html",{id:id},function (data) {
            dgg.Open({
                title: '预约详情',
                content: data ,
                area: '70%',
                btn:["取消"],
                yes:function (index) {
                    layer.close(index);
                    DGG.loading('close');
                }
            });
        },'html');
}


/**
 *  资质匹配
 *  2018/8/27  10:09
 *  mqy   qualificationMatch
 **/
function qualificationMatch(tableid,id) {
    if (null != tableid && "" !== tableid) {//从列表进来
        var ids = scCommonGetCheckedallproorder(tableid);
        if (ids.length > 1) {
            DGG.Warning('只能选择一个订单！');
            return;
        }
        if (ids.length == 0) {
            DGG.Warning('请选择一个订单！');
            return;
        }
        DGG.loading();
        $.post( sysInfo.basePath+"/zzsc/inprocess/qualification_matching_page.html",{id:ids[0]},function (data) {
            dgg.Open({
                title: '资质匹配',
                content: data ,
                area: '80%',
                btn:["取消"],
                success:function(layero, index){
                    $('#layerIndex').val(index);
                },
                yes:function (index) {
                    layer.close(index);
                    DGG.loading('close');
                }
            });
        },'html');
    } else {//从详情进来
        if (checkIsEmpty(id)){
            DGG.Warning('未获取到生产订单ID,请刷新列表！');
            return;
        }
        DGG.loading();
        $.post( sysInfo.basePath+"/zzsc/inprocess/qualification_matching_page.html",{id:id},function (data) {
            dgg.Open({
                title: '资质匹配',
                content: data ,
                area: '80%',
                btn:["取消"],
                success:function(layero, index){
                    $('#layerIndex').val(index);
                },
                yes:function (index) {
                    layer.close(index);
                    DGG.loading('close');
                }
            });
        },'html');
    }
}

/**
 *  资质匹配弹出框中的预约按钮
 *  2018/8/27  10:09
 *  mqy
 **/
function qualificationAppointment(id) {
    DGG.loading();
    $.post( sysInfo.basePath+"/zzsc/inprocess/qualification_appointment_page.html",{id:id},function (data) {
        dgg.Open({
            title: '预约详情',
            content: data ,
            area: '70%',
            btn:["取消"],
            yes:function (index) {
                layer.close(index);
                DGG.loading('close');
            }
        });
    },'html');
}

/**
 *  资质匹配弹出框中的匹配按钮
 *  2018/8/27  10:09
 *  mqy
 **/
function qualificationAdvanceMatch(id) {
    DGG.loading();
    $.post( sysInfo.basePath+"/zzsc/inprocess/qualification_advance_match_page.html",{id:id},function (data) {
        dgg.Open({
            title: '匹配详情',
            content: data ,
            area: '80%',
            btn:["取消"],
            yes:function (index) {
                layer.close(index);
                DGG.loading('close');
            }
        });
    },'html');
}

//资质取消预定
function cancelAdvanceQualification(certificateId,certificateNo,scOrderId,pageType,obj) {//pageType 1列表 2 详情  obj详情不用传,列表传,用于寻找tr
    DGG.Confire("确定取消预定此资质吗？", '取消预定', function () {
        DGG.loading();
        $.ajax({
            type: 'POST',
            data: {certificateId:certificateId,certificateNo:certificateNo,scOrderId:scOrderId,type:2},//type 1是证书,2是资质
            url: sysInfo.basePath + '/zzsc/inprocess/cancel_advance_certificate.do',
            dataType: 'JSON',
            success: function (data) {
                DGG.loading('close');
                if (data.code != 0) {
                    DGG.Errors(data.msg);
                } else {

                    if (pageType == 1){
                        $(obj).parents('.advanceClassTr').remove();
                    } else if (pageType == 2){
                        window.location.reload();
                    }
                    $.ajax({//异步获取已预定证书数量
                        type: 'POST',
                        data: {scOrderId:scOrderId,type:2},//type 1是证书,2是资质
                        url: sysInfo.basePath + '/zzsc/inprocess/query_advance_match_number.do',
                        dataType: 'JSON',
                        success: function (data) {
                            if (data.code != 0) {
                                $('.yudingClass').hide();
                                $('.yuyueClass').hide();
                            } else {
                                var cpNumber = data.data.cpNumber;
                                if (parseInt(cpNumber) > 0){//预定
                                    $('.yudingClass').show();
                                    $('#reservationNum').text(cpNumber);
                                }else {
                                    $('.yudingClass').hide();
                                }
                                var rnNumber = data.data.rnNumber;
                                if (parseInt(rnNumber) > 0){
                                    $('.yuyueClass').show();
                                    $('#appointmentNum').text(rnNumber);
                                } else {
                                    $('.yuyueClass').hide();
                                }
                            }
                        }
                    });
                    DGG.Succeed("操作成功!");
                }
            },
            error: function () {
            }
        });
    });
}
//资质匹配
function matchAdvanceQualification(certificateId,certificateNo,scOrderId,pageType,obj) {//pageType 1列表 2 详情  obj详情不用传,列表传,用于寻找tr
    DGG.Confire("确定匹配此资质吗？", '匹配资质', function () {
        DGG.loading();
        $.ajax({
            type: 'POST',
            data: {certificateId:certificateId,certificateNo:certificateNo,scOrderId:scOrderId,type:2},//type 1是证书,2是资质
            url: sysInfo.basePath + '/zzsc/inprocess/save_match_certificate.do',
            dataType: 'JSON',
            success: function (data) {
                DGG.loading('close');
                if (data.code != 0) {
                    DGG.Errors(data.msg);
                } else {

                    if (pageType == 1){
                        $(obj).parents('.advanceClassTr').remove();
                    } else if (pageType == 2){
                        window.location.reload();
                    }
                    $.ajax({//异步获取已预定证书数量
                        type: 'POST',
                        data: {scOrderId:scOrderId,type:2},//type 1是证书,2是资质
                        url: sysInfo.basePath + '/zzsc/inprocess/query_advance_match_number.do',
                        dataType: 'JSON',
                        success: function (data) {
                            if (data.code != 0) {
                                $('.yudingClass').hide();
                                $('.yuyueClass').hide();
                            } else {
                                var cpNumber = data.data.cpNumber;
                                if (parseInt(cpNumber) > 0){//预定
                                    $('.yudingClass').show();
                                    $('#reservationNum').text(cpNumber);
                                }else {
                                    $('.yudingClass').hide();
                                }
                                var rnNumber = data.data.rnNumber;
                                if (parseInt(rnNumber) > 0){
                                    $('.yuyueClass').show();
                                    $('#appointmentNum').text(rnNumber);
                                } else {
                                    $('.yuyueClass').hide();
                                }
                            }
                        }
                    });
                    DGG.Succeed("操作成功!");
                }
            },
            error: function () {
            }
        });
    });
}

//预约资质
function appointmentQualification() {
    var certificateType = $("#certificateTypeHigh").val();
    if (checkIsEmpty(certificateType)){
        DGG.Warning("请选择资质类型!");
        return;
    }
    var certificateNameHigh = $("#certificateNameHigh").val();
    if (checkIsEmpty(certificateNameHigh)){
        DGG.Warning("请选择资质名称!");
        return;
    }
    var certificateLevel = $("#certificateLevelHigh").val();
    if (checkIsEmpty(certificateLevel)){
        DGG.Warning("请选择资质等级!");
        return;
    }
    var applicationScopes = $("#applicationScopesHigh").val();
    if (checkIsEmpty(applicationScopes)){
        DGG.Warning("请选择应用范围!");
        return;
    }
    var appointmentAreaId = $("#appointmentAreaId").val();
    if (checkIsEmpty(appointmentAreaId)){
        DGG.Warning("请选择地区!");
        return;
    }
    var serializeObject = $('#myForm').serializeObject();
    //预约证书
    DGG.Confire("确定预约资质吗？", '预约资质', function () {
        DGG.loading();
        $.ajax({
            type: 'POST',
            data: serializeObject,
            url: sysInfo.basePath + '/zzsc/inprocess/save_appointment_qualification.do',
            dataType: 'JSON',
            success: function (data) {
                var $s = $('.myFormClass');
                $s.find('input').val('');
                $s.find('select').val('');
                $s.find('select').val(null).trigger("change");
                DGG.loading('close');
                if (data.code != 0) {
                    DGG.Errors(data.msg);
                } else {
                    DGG.Succeed("操作成功!");
                    appoinmentTable.ajax.reload();
                    $.ajax({//异步获取已预定证书数量
                        type: 'POST',
                        data: {scOrderId:serializeObject.scProductId,type:2},//type 1是证书,2是资质
                        url: sysInfo.basePath + '/zzsc/inprocess/query_advance_match_number.do',
                        dataType: 'JSON',
                        success: function (data) {
                            if (data.code != 0) {
                                $('.yudingClass').hide();
                                $('.yuyueClass').hide();
                            } else {
                                var cpNumber = data.data.cpNumber;
                                if (parseInt(cpNumber) > 0){//预定
                                    $('.yudingClass').show();
                                    $('#reservationNum').text(cpNumber);
                                }else {
                                    $('.yudingClass').hide();
                                }
                                var rnNumber = data.data.rnNumber;
                                if (parseInt(rnNumber) > 0){
                                    $('.yuyueClass').show();
                                    $('#appointmentNum').text(rnNumber);
                                } else {
                                    $('.yuyueClass').hide();
                                }
                            }
                        }
                    });
                }
            },
            error: function () {
            }
        });
    });
}
//取消证书或者资质的预约
function deleteProductReservationBySc(scOrderId,type) {
    var tit;
    if (type === 1){
        tit='确认取消该生产订单预约的所有证书吗?'
    } else {
        tit='确认取消该生产订单预约的所有资质吗?'
    }
    //预约证书
    DGG.Confire(tit, '取消所有预约', function () {
        DGG.loading();
        $.ajax({
            type: 'POST',
            data: {scOrderId:scOrderId,type:type},
            url: sysInfo.basePath + '/zzsc/inprocess/delete_product_reservation_by_sc.do',
            dataType: 'JSON',
            success: function (data) {
                DGG.loading('close');
                if (data.code != 0) {
                    DGG.Errors(data.msg);
                } else {
                    DGG.Succeed("操作成功!");
                    $.ajax({//异步获取已预定证书数量
                        type: 'POST',
                        data: {scOrderId:scOrderId,type:type},//type 1是证书,2是资质
                        url: sysInfo.basePath + '/zzsc/inprocess/query_advance_match_number.do',
                        dataType: 'JSON',
                        success: function (data) {
                            if (data.code != 0) {
                                $('.yudingClass').hide();
                                $('.yuyueClass').hide();
                            } else {
                                var cpNumber = data.data.cpNumber;
                                if (parseInt(cpNumber) > 0){//预定
                                    $('.yudingClass').show();
                                    $('#appointmentNum').text(cpNumber);
                                }else {
                                    $('.yudingClass').hide();
                                }
                                var rnNumber = data.data.rnNumber;
                                if (parseInt(rnNumber) > 0){
                                    $('.yuyueClass').show();
                                    $('#appointmentNum').text(rnNumber);
                                } else {
                                    $('.yuyueClass').hide();
                                }
                            }
                        }
                    });
                }
            },
            error: function () {
            }
        });
    });
}
////取消证书或者资质的预约
function cancelAdvanceBaseCredentialBySc(scOrderId,type) {
    var tit;
    if (type === 1){
        tit='确认取消该生产订单预定的所有证书吗?'
    } else {
        tit='确认取消该生产订单预定的所有资质吗?'
    }
    //预约证书
    DGG.Confire(tit, '取消所有预定', function () {
        DGG.loading();
        $.ajax({
            type: 'POST',
            data: {scOrderId:scOrderId,type:type},
            url: sysInfo.basePath + '/zzsc/inprocess/cancel_advance_base_credential_by_sc.do',
            dataType: 'JSON',
            success: function (data) {
                DGG.loading('close');
                if (data.code != 0) {
                    DGG.Errors(data.msg);
                } else {
                    DGG.Succeed("操作成功!");
                    $.ajax({//异步获取已预定证书数量
                        type: 'POST',
                        data: {scOrderId:scOrderId,type:type},//type 1是证书,2是资质
                        url: sysInfo.basePath + '/zzsc/inprocess/query_advance_match_number.do',
                        dataType: 'JSON',
                        success: function (data) {
                            if (data.code != 0) {
                                $('.yudingClass').hide();
                                $('.yuyueClass').hide();
                            } else {
                                var cpNumber = data.data.cpNumber;
                                if (parseInt(cpNumber) > 0){//预定
                                    $('.yudingClass').show();
                                    $('#appointmentNum').text(cpNumber);
                                }else {
                                    $('.yudingClass').hide();
                                }
                                var rnNumber = data.data.rnNumber;
                                if (parseInt(rnNumber) > 0){
                                    $('.yuyueClass').show();
                                    $('#appointmentNum').text(rnNumber);
                                } else {
                                    $('.yuyueClass').hide();
                                }
                            }
                        }
                    });
                }
            },
            error: function () {
            }
        });
    });
}

/**
 *  获取选择的订单id
 *  2018/8/13 10:09
 *  maoquanyi
 **/
function scCommonGetCheckedallproorder(tableid) {
    var datas = $('#' + tableid).dataTableExtend('getCheck');
    var ids = [];
    for (var i = 0; i < datas.length; i++) {
        ids.push(datas[i].id);
    }
    return ids;
}
/**
 *  强制完结订单
 *  2018/8/16  10:18
 *  maoquanyi
 */
function forceOver(tableid, id) {
    if (null != tableid && "" != tableid) {
        var ids = scCommonGetCheckedallproorder(tableid);
        if (ids.length > 1) {
            DGG.Warning('只能选择一个订单！');
            return;
        }
        if (ids.length == 0) {
            DGG.Warning('请选择一个订单！');
            return;
        }
        DGG.loading();
        $.post( sysInfo.basePath+"/zzsc/inprocess/force_order_remarks.html",function (data) {
            dgg.Open({
                title: '强制完结备注',
                content: data ,
                area: '500px',
                btn:["确认","取消"],
                yes:function () {
                    commitForceRemark(ids[0]);
                }
            });
        },'html');
    }

    /**
     * 保存强制完结备注
     * @param scorderId
     */
    function commitForceRemark(id){
        var remark = $("#remark").val();
        if (checkIsEmpty(remark)){
            DGG.Errors("备注不能为空!");
            return;
        }
        DGG.Confire("确认保存吗？", function () {
            DGG.loading();
            $.ajax({
                url: '/zzsc/inprocess/force_over_order.do',
                data: {remark: remark, scorderId: id},
                type: "POST",
                success: function (result) {
                    DGG.loading('close');
                    if (result.code == 0) {
                        DGG.Succeed('操作成功!');
                        window.location.reload();
                    } else {
                        DGG.Errors(result.msg);
                    }
                }
            });

        });
    }
}

/**
 * 创建企管家工单
 * @param tableid
 */
function openCreateWorkOrderPage(tableid) {

    if (null != tableid && "" !== tableid) {
        var datas = $('#' + tableid).dataTableExtend('getCheck');
        if (datas.length === 0) {
            DGG.Warning('请选择一个订单！');
            return;
        }
        if (datas.length > 1) {
            DGG.Warning('只能选择一个订单！');
            return;
        }
    }
    var type='CPMPLAIN'; //'CPMPLAIN';//工单类型
    var customerId =datas[0].customerId;//客户ID
    var customerNo = datas[0].customerNo;//客户编号
    var customerName=datas[0].customerName;//客户名称
    var  customerWay = 'MB';//客户联系方式
    var customerNumber = datas[0].encryptionPhone;//;//客户联系电话 密文
    var businessTypeId=datas[0].businessTypeCode;//一级业态
    //var businessTypeName = datas[0].businessTypeName;//业态名称
    var businessId=datas[0].id; //关联业务id
    var businessNumber=datas[0].scProductOrderNo;////关联业务编号
    var source = 'BACKGROUND_PRODUCTION'; //工单来源 REVIEW
    var unionModule='PRODUCTION_ORDER';//关联模块
    var businessSystem='QGJ_ZZSC';//业务系统
    var degree;//紧急程度
    var content;//工单内容
    var stewardId;//管家ID
    var stewardName;//管家姓名/工号
    var stewardOrgId;//管家部门id
    var returnUrl;//工单处理中各节点的回调地址

    if (checkIsEmpty(customerNumber)) {
        DGG.Layer.message('客户联系方式(电话密文)不能为空！', {icon: '5'});
        return;
    }
// debugger;
   window.parent.updateTab('创建客服工单',qgjWoUrl+'/qgj/wo/order/create_work_order.html?type='
       +type+'&customerId='+customerId+'&customerNo='+customerNo+'&customerName='+customerName+'&customerWay='
       +customerWay+'&customerNumber='+customerNumber+'&source='+source+'&unionModule='+unionModule+'&businessSystem='
       +businessSystem+'&businessId='+businessId+'&businessNumber='+businessNumber+'&degree='+degree+'&content='
       +content+'&stewardId='+stewardId+'&stewardName='+stewardName+'&stewardOrgId='+stewardOrgId+'&returnUrl='
       +returnUrl+'&businessTypeId='+businessTypeId);

    //window.parent.assignClose('创建客服工单');
}

/**
 * 创建企管家工单
 * @param tableid
 */
function openCreateWorkQgjOrderPage(customerId,customerNo,customerName,customerNumber,businessTypeId,businessId,businessNumber) {
        // debugger;

    var type='CPMPLAIN'; //'CPMPLAIN';//工单类型
    var customerId =customerId;//客户ID
    var customerNo = customerNo;//客户编号
    var customerName=customerName;//客户名称
    var  customerWay = 'MB';//客户联系方式
    var customerNumber = customerNumber;//;//客户联系电话 密文
    var businessTypeId=businessTypeId;//一级业态
    //var businessTypeName = datas[0].businessTypeName;//业态名称
    var businessId=businessId; //关联业务id
    var businessNumber=businessNumber;////关联业务编号
    var source = 'BACKGROUND_PRODUCTION'; //工单来源 REVIEW
    var unionModule='PRODUCTION_ORDER';//关联模块
    var businessSystem='QGJ_ZZSC';//业务系统
    var degree;//紧急程度
    var content;//工单内容
    var stewardId;//管家ID
    var stewardName;//管家姓名/工号
    var stewardOrgId;//管家部门id
    var returnUrl;//工单处理中各节点的回调地址

    if (checkIsEmpty(customerNumber)) {
        DGG.Layer.message('客户联系方式(电话密文)不能为空！', {icon: '5'});
        return;
    }

    window.parent.updateTab('创建客服工单',qgjWoUrl+'/qgj/wo/order/create_work_order.html?type='
        +type+'&customerId='+customerId+'&customerNo='+customerNo+'&customerName='+customerName+'&customerWay='
        +customerWay+'&customerNumber='+customerNumber+'&source='+source+'&unionModule='+unionModule+'&businessSystem='
        +businessSystem+'&businessId='+businessId+'&businessNumber='+businessNumber+'&degree='+degree+'&content='
        +content+'&stewardId='+stewardId+'&stewardName='+stewardName+'&stewardOrgId='+stewardOrgId+'&returnUrl='
        +returnUrl+'&businessTypeId='+businessTypeId);
}



